/*
* @Author: dengjiayao
* @Date:   2017-08-28 16:57:27
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-12-21 17:44:59
*/
import Pages from '../../component/pages/pages.vue'
import { showMsg, getClassesList, deleteClasses } from '../../vuex/action'

export default {
  name: 'classes-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.classesListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getClassesList,
      xDelete: deleteClasses
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({ start = this.pages.start, limit = this.pages.limit }) {
      this.xGetList({
        start: start,
        limit: limit
      })
        .then(data => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateItem({
      id,
      top,
      title,
      summary,
      listPicUrl,
      headPicUrl,
      shopPicUrl,
      shopUrl,
      topicId,
      frequency,
      highlights,
      lockedInfo,
      timetablePicUrl,
      lecturerIntro,
      suitableFor
    }) {
      let ref = this.$root.$refs.classesConfig
      let fileFields = [
        'listPicFile',
        'headPicFile',
        'shopPicFile',
        'timetablePicFile'
      ]
      for (let k of fileFields) {
        ref.$els[k].value = ''
      }
      ref.actionType = 'UPDATE'
      ref.form = {
        id,
        top,
        title,
        summary,
        listPicUrl,
        headPicUrl,
        shopPicUrl,
        shopUrl,
        topicId,
        frequency,
        highlights,
        lockedInfo,
        timetablePicUrl,
        lecturerIntro,
        suitableFor
      }
      ref.showModal = true
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      }).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
